package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.entity.MarketOrder
import com.zxy.supplier_system.server.service.MarketOrderService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice
import org.springframework.data.domain.Sort
import org.springframework.data.web.SortDefault
import org.springframework.web.bind.annotation.*
import java.time.OffsetDateTime

@RestController
@RequestMapping("markets/{marketId}/orders")
class MarketsOrderController(
    private val marketOrderService: MarketOrderService
) {

    @HasAnyAuthority(Authority.卖场_订单_下单)
    @PostMapping
    fun createMarketOrder(@PathVariable marketId: Int): Int {
        return this.marketOrderService.createMarketOrder(marketId)
    }

    @HasAnyAuthority(Authority.卖场_订单_查询)
    @GetMapping
    fun queryMarketOrderInMarket(
        @PathVariable marketId: Int,
        @SortDefault(
            sort = ["createdDateTime"],
            direction = Sort.Direction.DESC
        ) pageable: Pageable
    ): Slice<QueryMarketOrderInMarketResponse> {
        return this.marketOrderService.queryMarketOrderInMarket(marketId, pageable)
    }

    data class QueryMarketOrderInMarketResponse(
        val id: Int,
        val createdBy: IntIdAndName,
        val createdDateTime: OffsetDateTime,
        val goodsKindAmount: Int,
        val submittedDateTime: OffsetDateTime?,
        val status: MarketOrder.Status,
    )

    @GetMapping("employees", params = ["projection=summary"])
    fun queryOrderCreatedEmployeesByMarketId(@PathVariable marketId: Int): List<IntIdAndName> {
        return this.marketOrderService.queryOrderCreatedEmployeesByMarketId(marketId)
    }


}